﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Net;
using System.Xml.Linq;

namespace WpfApplication31
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            TweetList.ItemsSource = _tweets;
        }

        private ObservableCollection<Tweet> _tweets = new ObservableCollection<Tweet>();

        private void GetTweets_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();

            client.DownloadStringCompleted += (s, ea) =>
            {
                XDocument doc = XDocument.Parse(ea.Result);

                XNamespace ns = "http://www.w3.org/2005/Atom"; 

                var items = from item in doc.Descendants(ns + "entry")
                            select new Tweet()
                            {
                                Title = item.Element(ns + "title").Value,

                                Image = new Uri((from XElement xe in item.Descendants(ns + "link")
                                                    where xe.Attribute("type").Value == "image/png"
                                                    select xe.Attribute("href").Value).First<string>()),

                                Link = new Uri((from XElement xe in item.Descendants(ns + "link")
                                                where xe.Attribute("type").Value == "text/html"
                                                select xe.Attribute("href").Value).First<string>()),
                            };

                foreach (Tweet t in items)
                {
                    _tweets.Add(t);
                }

                UpdateOverlayIcon();
            };

            client.DownloadStringAsync(new Uri("http://search.twitter.com/search.atom?q=wpf"));
        }

        private class Context
        {
            public string CountText { get; set; }
        }

        private void UpdateOverlayIcon()
        {
            int iconWidth = 20;
            int iconHeight = 20;

            Context context = new Context();
            if (_tweets.Count > 99)
                context.CountText = "+";
            else
                context.CountText = _tweets.Count.ToString();


            RenderTargetBitmap bmp = 
                new RenderTargetBitmap(iconWidth, iconHeight, 96, 96, PixelFormats.Default);

            ContentControl root = new ContentControl();

            root.ContentTemplate = ((DataTemplate)Resources["OverlayIcon"]);
            root.Content = context;

            root.Arrange(new Rect(0, 0, iconWidth, iconHeight));

            bmp.Render(root);

            TaskbarItemInfo.Overlay = (ImageSource)bmp;
        }

    }

}
